#include "../log/log.h"

// void test_log()
// {
//     log::Logger::ptr logger = log::LoggerManager::getInstance().getLogger("async_logger");
//     logger->debug(__FILE__, __LINE__, "%s", "测试日志");
//     logger->info(__FILE__, __LINE__, "%s", "测试日志");
//     logger->warn(__FILE__, __LINE__, "%s", "测试日志");
//     logger->error(__FILE__, __LINE__, "%s", "测试日志");
//     logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

//     size_t cursize = 0, count = 0;
//     while (cursize < 500000)
//     {
//         logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
//         cursize ++;
//     }
// }

void test_log(const std::string& name)
{
    INFO("%s","测试开始");
    log::Logger::ptr logger = log::LoggerManager::getInstance().getLogger(name);
    logger->debug("%s", "测试日志");
    logger->info("%s", "测试日志");
    logger->warn( "%s", "测试日志");
    logger->error("%s", "测试日志");
    logger->fatal("%s", "测试日志");
    INFO("%s","测试结束");
    // size_t cursize = 0, count = 0;
    // while (cursize < 500000)
    // {
    //     logger->fatal("测试日志-%d",count++);
    //     cursize ++;
    // }

    // DEBUG("%s", "测试日志");
    // INFO("%s", "测试日志");
    // WARN( "%s", "测试日志");
    // ERROR("%s", "测试日志");
    // FATAL("%s", "测试日志");

    // size_t cursize = 0, count = 0;
    // while (cursize < 500000)
    // {
    //     FATAL("测试日志-%d",count++);
    //     cursize ++;
    // }
}
int main()
{
    // std::cout << log::util::Date::getTime() << std::endl;
    // std::string pathname ="./abc/bcd/a.txt";
    // log::util::File::createDirectory(pathname);

    // std::cout << log::LogLevel::toString(log::LogLevel::value::DEBUG) << std::endl;
    // std::cout << log::LogLevel::toString(log::LogLevel::value::FATAL) << std::endl;
    // std::cout << log::LogLevel::toString(log::LogLevel::value::INFO) << std::endl;

    // log::LogMessage msg(log::LogLevel::value::INFO, 53, "main.c", "root", "格式化功能测试...");
    // // log::Formatter fmt("abc%%abc[%d{%H:%M:%S}]%m%n");
    // log::Formatter fmt;
    // std::string str = fmt.format(msg);
    // std::cout << str;

    // log::LogSink::ptr stdoutSink = log::SinkFactory::create<log::StdoutSink>();
    // stdoutSink->log(str.c_str(), str.size());

    // log::LogSink::ptr fileSink = log::SinkFactory::create<log::FileSink>("./logfile/test.log");
    // // std::cout << "创建OK2" << std::endl;
    // fileSink->log(str.c_str(), str.size());
    // // std::cout << "写入OK2" << std::endl;

    // log::LogSink::ptr rollSink = log::SinkFactory::create<log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);
    // // std::cout << "创建OK3" << std::endl;

    // size_t cursize = 0;
    // size_t count = 0;
    // while (cursize < 1024 * 1024 * 10)
    // {
    //     std::string tmp = str + std::to_string(count++);

    //     rollSink->log(tmp.c_str(), tmp.size());
    //     cursize += tmp.size();
    // }

    // log::LogSink::ptr timesink = log::SinkFactory::create<RollByTimeSink>("./logfile/roll-", TimeGap::GAP_SECOND);
    // time_t old = log::util::Date::getTime();
    // while (log::util::Date::getTime() < old + 5)
    // {
    //     timesink->log(str.c_str(), str.size());
    // }

    // std::string logger_name = "sync_logger";
    // log::LogLevel::value limit = log::LogLevel::value::WARN;
    // log::Formatter::ptr fmt(new log::Formatter());
    // log::LogSink::ptr stdoutSink = log::SinkFactory::create<log::StdoutSink>();
    // log::LogSink::ptr fileSink = log::SinkFactory::create<log::FileSink>("./logfile/test.log");
    // log::LogSink::ptr rollSink = log::SinkFactory::create<log::RollBySizeSink>("./logfile/roll-", 1024 * 1024);

    // std::vector<log::LogSink::ptr> sinks = {stdoutSink, fileSink, rollSink};
    // log::Logger::ptr logger(new log::SyncLogger(logger_name, limit, fmt, sinks));

    // std::unique_ptr<log::LoggerBuilder> builder(new log::LocalLoggerBuilder());
    // builder->buildLoggerName("sync_logger");
    // builder->buildLoggerLevel(log::LogLevel::value::WARN);
    // builder->buildLoggerType(log::LoggerType::LOGGER_SYNC);
    // builder->buildFormatter("%m%n");
    // builder->buildSink<log::StdoutSink>();
    // builder->buildSink<log::FileSink>("./logfile/test.log");
    // log::Logger::ptr logger = builder->build();

    // logger->debug(__FILE__, __LINE__, "%s", "测试日志");
    // logger->info(__FILE__, __LINE__, "%s", "测试日志");
    // logger->warn(__FILE__, __LINE__, "%s", "测试日志");
    // logger->error(__FILE__, __LINE__, "%s", "测试日志");
    // logger->fatal(__FILE__, __LINE__, "%s", "测试日志");

    // size_t cursize = 0, count = 0;
    // while (cursize < 1024 * 1024 * 10)
    // {
    //     logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
    //     cursize += 20;
    // }

    //(buffer测试)
    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if (ifs.is_open() == false)
    // {
    //     std::cout << "open fail" << std::endl;
    //     return -1;
    // }
    // ifs.seekg(0, std::ios::end); // 读写位置跳转到文件末尾
    // size_t fsize = ifs.tellg();  // 获取当前位置相对于起始位置的偏移量
    // ifs.seekg(0, std::ios::beg); // 重新跳转到起始位置
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);

    // if (ifs.good() == false)
    // {
    //     std::cout << "read error\n";
    //     return -1;
    // }
    // ifs.close();

    // log::Buffer buffer;
    // for (int i = 0; i < body.size(); ++i)
    // {
    //     buffer.push(&body[i], 1);
    // }
    // std::cout << buffer.readableSize() << std::endl;

    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
    // size_t rsize = buffer.readableSize();
    // for (int i = 0; i < rsize; ++i)
    // {
    //     ofs.write(buffer.begin(), 1);

    //     buffer.moveReader(1);
    // }
    // ofs.close();


    std::unique_ptr<log::LoggerBuilder> builder(new log::GlobalLoggerBuilder());
    builder->buildLoggerName("async_logger");
    builder->buildLoggerLevel(log::LogLevel::value::DEBUG);
    builder->buildLoggerType(log::LoggerType::LOGGER_SYNC);//同步日志器
    //builder->buildLoggerType(log::LoggerType::LOGGER_ASYNC);//异步日志器类型
    //builder->buildEnableUnsafeAsync();//异步日志器设置为不安全模式
    builder->buildFormatter("[%c][%f:%l][%p]%m%n");
    builder->buildSink<log::StdoutSink>();
    builder->buildSink<log::FileSink>("./logfile/sync.log");
    builder->buildSink<log::RollBySizeSink>("./logfile/roll-sync-by-size",1024*1024);
    builder->build();

    test_log("async_logger");
}